package com.example.service.impl;


import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.enetity.Article;
import com.example.mapper.ArticleMapper;
import com.example.service.ArticleService;
import com.example.utils.getRedisDataUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.Map;

@Service
public class ArticleServiceImpl extends ServiceImpl<ArticleMapper, Article> implements ArticleService {
    @Autowired
    private ArticleMapper mapper;
    @Autowired
    private StringRedisTemplate template;

    @Override
    public void add(Article article) {
        String s = template.opsForValue().get("data");
        JSONObject jsonObject = JSONObject.parseObject(s);
        Map<String,Object> map = (Map<String,Object>)jsonObject;
        article.setCreateTime(LocalDateTime.now());
        article.setUpdateTime(LocalDateTime.now());
        article.setCreateUser( (Integer) map.get("id"));
        mapper.insert(article);

    }

    @Override
    public IPage<Article> getPage(Integer id, Integer pageNum, Integer pageSize, String categoryId, String state) {
        IPage<Article> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<Article> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Article::getCreateUser,id);
        wrapper.eq(StringUtils.hasLength(categoryId),Article::getCategoryId, categoryId);
        wrapper.eq(StringUtils.hasLength(state),Article::getState,state);
        return mapper.selectPage(page,wrapper);
    }

    @Override
    public Article getOneById(Integer id) {
        return mapper.selectById(id);
    }

}
